package com.ruoyi.jwxt.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.StudentStatusConstants;
import com.ruoyi.jwxt.domain.PO.JwxtStudentApplication;
import com.ruoyi.jwxt.mapper.JwxtStudentApplicationMapper;
import com.ruoyi.jwxt.service.IJwxtLabSchedulingService;
import com.ruoyi.jwxt.service.IJwxtStudentApplicationService;

/**
 * 学生实验课申请登记Service业务层处理
 */
@Service
public class JwxtStudentApplicationServiceImpl implements IJwxtStudentApplicationService {
    @Autowired
    private JwxtStudentApplicationMapper jwxtStudentExperimentApplicationMapper;

    @Autowired
    private IJwxtLabSchedulingService jwxtLabSchedulingService;

    /**
     * 新增申请，填写新的申请，新申请的状态为“未审核”
     * 申请的内容包括：
     * 申请学期、申请学生、申请周次、申请节次、申请实验室编号(应根据学期、周次、节次检查是否可用)、申请原因、填报日期
     */
    @Override
    public int insert(JwxtStudentApplication jwxtStudentExperimentApplication) {
        if (jwxtLabSchedulingService.getFreeLabs(jwxtStudentExperimentApplication).size() > 0) {
            jwxtStudentExperimentApplication.setApplicationStatus(StudentStatusConstants.PENDING);
            jwxtStudentExperimentApplication.setFillDate(new Date());
            return jwxtStudentExperimentApplicationMapper.insert(jwxtStudentExperimentApplication);
        }
        return 0;
    }

    /**
     * 批量删除学生实验课申请登记
     */
    @Override
    public int delete(Long[] applicationIds) {
        return jwxtStudentExperimentApplicationMapper.delete(applicationIds);
    }

    /**
     * 删除单个学生实验课申请登记
     */
    @Override
    public int delete(Long applicationId) {
        Long[] applicationIds = { applicationId };
        return jwxtStudentExperimentApplicationMapper.delete(applicationIds);
    }

    /**
     * 查询学生实验课申请登记列表
     */
    @Override
    public List<JwxtStudentApplication> select(JwxtStudentApplication jwxtStudentExperimentApplication) {
        return jwxtStudentExperimentApplicationMapper.select(jwxtStudentExperimentApplication);
    }

    /**
     * 查询学生实验课申请登记
     */
    @Override
    public JwxtStudentApplication select(Long applicationId) {
        JwxtStudentApplication jwxtStudentExperimentApplication = new JwxtStudentApplication();
        jwxtStudentExperimentApplication.setApplicationId(applicationId);
        return jwxtStudentExperimentApplicationMapper.select(jwxtStudentExperimentApplication).get(0);
    }

    /**
     * 修改申请，对处于“未审核”状态的申请可以修改内容
     */
    @Override
    public int update(JwxtStudentApplication jwxtStudentExperimentApplication) {
        if (jwxtStudentExperimentApplication.getApplicationStatus().equals(StudentStatusConstants.PENDING)) {
            return jwxtStudentExperimentApplicationMapper.update(jwxtStudentExperimentApplication);
        }
        return 0;
    }

    /**
     * 对于通过的申请，在使用完成后进行确认(将申请状态设置为“使用完毕”)
     */
    @Override
    public int confirm(JwxtStudentApplication jwxtStudentExperimentApplication) {
        if (jwxtStudentExperimentApplication.getApplicationStatus().equals(StudentStatusConstants.ACCEPTED)) {
            jwxtStudentExperimentApplication.setApplicationStatus(StudentStatusConstants.USED);
            return jwxtStudentExperimentApplicationMapper.update(jwxtStudentExperimentApplication);
        }
        return 0;
    }

}
